from time import sleep
import socket
import network


class Wlan:
  
  def __init__(self,ssid,passwd):
    self.ssid = ssid
    self.passwd = passwd
    self.state = False

  def connect_wifi(self):
    self.WLAN = network.WLAN(network.STA_IF)
    self.WLAN.active(True)
    
    for i in self.WLAN.scan():
      if i[0].decode() == self.ssid:
        self.WLAN.connect(self.ssid,self.passwd)
        break
    else:
      print("没有扫描到Wifi")
      return False
    
    for i in range(5):
      if self.WLAN.isconnected():
        break
      sleep(1)
    self.state = self.WLAN.isconnected()
    return self.state


  def isConnected(self):
    self.state = self.WLAN.isconnected()
    return self.state

